using System;
using System.Collections.Generic;
using Runtime.Common.Interface.GameWorld.Core.Interface;
using Runtime.Common.Object;
using Runtime.ObjectBehaviour.Interface;
using Runtime.Data.Interface;
using UnityEngine;

namespace Runtime.Module.GameGraphic
{
    [Serializable]
    public class GameObjectNodeInfoCtrl<T> : IInit where T : Component
    {
        [SerializeField] private List<GameObjectNodeInfo> list = new();
        private Dictionary<string, GameObjectNodeInfo> dict = new();

        public virtual void Init()
        {
            initDict();
        }

        private void initDict()
        {
            list.ForEach(info =>
            {
                if (dict.ContainsKey(info.Path)) Debug.LogError($"存在相同对象{info.Path}");

                if (!info.Node) return;

                dict[info.Path] = info;
            });
        }

        public T GetComponent(string path)
        {
            GameObjectNodeInfo temp;
            if (dict.TryGetValue(path, out temp)) return temp.Node;
            return null;
        }

        public List<V> GetComponents<V>() where V : T
        {
            return list.ConvertAll(info => (V)info.Node);
        }

        public List<T> GetNodes()
        {
            return list.ConvertAll(info => info.Node);
        }

        public List<GameObject> GetNodeOfGameObjects()
        {
            return list.ConvertAll(info => info.Node?.gameObject);
        }

        [Serializable]
        private class NodeInfo<TN> where TN : T
        {
            public string Path;
            public TN Node;
        }

        [Serializable]
        private class GameObjectNodeInfo : NodeInfo<T>
        {
        }
    }
}